package io.github.quickmsg.core.protocol;

import io.github.quickmsg.common.integrate.SubscribeTopic;
import io.github.quickmsg.common.integrate.cluster.IntegrateCluster;
import io.github.quickmsg.common.integrate.topic.IntegrateTopics;
import io.github.quickmsg.common.message.mqtt.ClusterMessage;

import java.util.Set;

public class BasePublish {

    /**
     * 发布通配符消息
     *
     * @param integrateCluster
     * @param topics
     * @param clusterMessage
     */
    public void publishWildcard(IntegrateCluster integrateCluster, IntegrateTopics<SubscribeTopic> topics, ClusterMessage clusterMessage) {
        if (topics.isWildcard(clusterMessage.getTopic())) {
            Set<String> wildcardTopics = topics.getWildcardTopics(clusterMessage.getTopic());
            if (wildcardTopics != null && !wildcardTopics.isEmpty()) {
                wildcardTopics.forEach(tp -> {
                    clusterMessage.setTopic(tp);
                    integrateCluster.sendCluster(tp, clusterMessage);
                });
            }
        }
    }
}
